package Gui;
import org.mortbay.jetty.Connector;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.util.HashMap;

import Command.MainCommand;
import DataBase.MenuTag_File;
import Log.Log;
import Observing.ObserverThread;
import Web.ForeverFrameServlet;
import Web.MainServlet;
import Web.RunWebThread;

import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import java.awt.Desktop;
import java.io.IOException;
import javax.swing.JFileChooser;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
* Description :mainGui
* <p>
*
*@author Danielle
*/
public class mainGui extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	private Composite mainView;
	private Menu menu1;
	private Menu menu2;
	private Button cancel;
	private MenuItem theState;
	private Menu menu6;
	private MenuItem state;
	private MenuItem delDB;
	private MenuItem stopListener;
	private MenuItem startListener;
	private MenuItem seeAll;
	private MenuItem remove;
	private MenuItem add;
	private MenuItem menuItem2;
	private MenuItem menuItem1;
	private Menu menu5;
	private StyledText text;
	private Composite freeText;
	private MenuItem log;
	private MenuItem system;
	private MenuItem updateAlgo;
	private MenuItem listener;
	private MenuItem initing;
	private MenuItem details;
	private MenuItem openAlgo;
	private Menu menu4;
	private Menu menu3;
	private Menu menuAlgo;
	private MenuItem init;
	private MenuItem algorithms;
	private int counterItem;
	private static MainCommand mainCommand;
	static HashMap <Integer,String> listListener;
	private MenuItem menuItemAlgo[];
	private static Shell shell;
	private MenuItem SelectByTag;
	private static BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("ProjectExec.xml"));	
	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args)throws Exception  {
	

		
		mainCommand=(MainCommand)beanFactory.getBean("MainCommand");
		RunWebThread thread=new RunWebThread(mainCommand);
		thread.start();		   
		showGUI();
	}
	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		listListener=new HashMap <Integer,String>() ;
		new Log();
		Display display = Display.getDefault();
		try {
			mainCommand=new MainCommand();
		} catch (Exception e) {
			e.printStackTrace();
		}
		shell = new Shell(display);
		mainGui inst = new mainGui(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public mainGui(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				menu1 = new Menu(getShell(), SWT.BAR);
				getShell().setMenuBar(menu1);
				{
					algorithms = new MenuItem(menu1, SWT.CASCADE);
					algorithms.setText("Algorithms For Scanning");
					{
						menu2 = new Menu(algorithms);
						algorithms.setMenu(menu2);
						{
							openAlgo = new MenuItem(menu2, SWT.PUSH);
							openAlgo.setText("Select Algorithms");
							openAlgo.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									String[] algo=SelectAlgo.showGUI(new File("algo"));
									if(algo!=null)
									{
										int size=algo.length;	
										mainCommand.deleteAllAlgo();
										for(int i=0; i<size; i++)
										{
											try {
												mainCommand.addAlgo((algo[i].split(".class"))[0]);
											} catch (Exception e) {
												e.printStackTrace();
											}
										}
									}
									
									
									
								}
							});
						}
						
						{
							details = new MenuItem(menu2, SWT.CASCADE);
                            details.setText("Details About The Algorithm");
                            final String[] list = (new File("algo")).list(new myFilter());
                            final int size=list.length;
                            menuItemAlgo=new MenuItem[size];//(getShell(), SWT.BAR);
                            menuAlgo = new Menu(details);
                            details.setMenu(menuAlgo);
                            int i;
                            for(i=0; i<size; i++)
                            {
                                menuItemAlgo[i]=new MenuItem(menuAlgo, SWT.PUSH);
                                menuItemAlgo[i].setText(list[i]);
                                final int place=i;
                                menuItemAlgo[i].addSelectionListener(new SelectionAdapter() {
									public void widgetSelected(SelectionEvent evt) {
										String str=""+list[place]+"  Description :\t";
										str+=mainCommand.algoDiscreption(list[place]);
										Details.showGUI(str);
										
									}
                                });
                                
                            }

						}
					}
				}
				{
					init = new MenuItem(menu1, SWT.CASCADE);
					init.setText("  Init The System");
					{
						menu3 = new Menu(init);
						init.setMenu(menu3);
						{
							initing = new MenuItem(menu3, SWT.PUSH);
							initing.setText("Init The System");
							initing.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									mainCommand.setFirstTimeStartInit();
									InitingSys.showGUI(mainCommand);
									
								}
							});
						}
						{
							delDB = new MenuItem(menu3, SWT.PUSH);
							delDB.setText("Delete DB");
							delDB.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									mainCommand.deleteDB();
								}
							});
						}
					}
				}
				{
					system = new MenuItem(menu1, SWT.CASCADE);
					system.setText("  The System Option");
					{
						menu4 = new Menu(system);
						system.setMenu(menu4);
						{
							listener = new MenuItem(menu4, SWT.CASCADE);
							listener.setText("Listener Options");
							{
								menu5 = new Menu(listener);
								listener.setMenu(menu5);
								{
									add = new MenuItem(menu5, SWT.PUSH);
									add.setText("Add a new folder  to the listener's list");
									add.addSelectionListener(new SelectionAdapter() {
										public void widgetSelected(SelectionEvent evt) {
											String fileFolder=AddToListener.showGUI();
											if (fileFolder!=null){
												listListener.put(counterItem, fileFolder);
												counterItem++;
												mainCommand.addSource(fileFolder);
											}
											
										}
									});
								}
								{
									remove = new MenuItem(menu5, SWT.PUSH);
									remove.setText("Remove folder from the listener's list");
									remove.addSelectionListener(new SelectionAdapter() {
										public void widgetSelected(SelectionEvent evt) {
											String[] fileFolder =RemoveData.showGUI(mainCommand);
											if (fileFolder!=null){
												int i,size=fileFolder.length;
												for(i=0; i<size; i++)
												{
													mainCommand.deleteSource(fileFolder[i]);
												}
												
											}
											
										}
									});
								}
								{
									seeAll = new MenuItem(menu5, SWT.PUSH);
									seeAll.setText("See all the folders  that are in the listener's list");
									seeAll.addSelectionListener(new SelectionAdapter() {
										public void widgetSelected(SelectionEvent evt) {
											String[] show=mainCommand.returnAllSource();
											
											if(show!=null)
											{
											
												ShowAll.showGUI(show);
											
											}
										}
									});
								}
								{
									startListener = new MenuItem(menu5, SWT.PUSH);
									startListener.setText("Start Listener");
									startListener.addSelectionListener(new SelectionAdapter() {
										public void widgetSelected(SelectionEvent evt) {
											
											if((mainCommand.returnAllSource()).length!=0)
											{
												theState.setText("        started        ");
												mainCommand.startObserver();
											}
												
										}
									});
								}
								{
									stopListener = new MenuItem(menu5, SWT.PUSH);
									stopListener.setText("Stop Listener");
									stopListener.addSelectionListener(new SelectionAdapter() {
										public void widgetSelected(SelectionEvent evt) {
											if((mainCommand.returnAllSource()).length!=0)
											{
												theState.setText("        stopped        ");
												mainCommand.stopObserver();
											}
												
										}
									});
								}
								{
									state = new MenuItem(menu5, SWT.CASCADE);
									state.setText("state of the the Listener:");
									{
										menu6 = new Menu(state);
										state.setMenu(menu6);
										{
											theState = new MenuItem(menu6, SWT.PUSH);
											theState.setText("        stopped        ");
										}
									}
								}

							}
						}
						{
							log = new MenuItem(menu4, SWT.PUSH);
							log.setText("The Log");
							log.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									
									Runtime myCommand = java.lang.Runtime.getRuntime();
									try {
										myCommand.exec("cmd /c \"" + "c:\\Log.txt" + "\"");
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
							});
						}
						{
							SelectByTag = new MenuItem(menu4, SWT.PUSH);
							SelectByTag.setText("Select By Tag");
							SelectByTag.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									SelectTag.showGUI(mainCommand);
									
								}
							});
						}
					}
				}
			}
			{
				mainView = new Composite(this, SWT.NONE);
				GridLayout mainViewLayout = new GridLayout();
				mainViewLayout.makeColumnsEqualWidth = true;
				FormData mainViewLData = new FormData();
				mainViewLData.left =  new FormAttachment(0, 1000, 0);
				mainViewLData.top =  new FormAttachment(0, 1000, 0);
				mainViewLData.width = 446;
				mainViewLData.height = 254;
				mainView.setLayoutData(mainViewLData);
				mainView.setLayout(mainViewLayout);
				{
					freeText = new Composite(mainView, SWT.NONE);
					FormLayout freeTextLayout = new FormLayout();
					GridData freeTextLData = new GridData();
					freeTextLData.widthHint = 437;
					freeTextLData.heightHint = 244;
					freeTextLData.horizontalIndent = 2;
					freeText.setLayoutData(freeTextLData);
					freeText.setLayout(freeTextLayout);
					{
						text = new StyledText(freeText, SWT.NONE);
						FormData textLData = new FormData();
						textLData.left =  new FormAttachment(1, 1000, 0);
						textLData.width = 430;
						textLData.height = 249;
						textLData.top =  new FormAttachment(2, 1000, 0);
						textLData.right =  new FormAttachment(1001, 1000, 0);
						textLData.bottom =  new FormAttachment(1022, 1000, 0);
						text.setLayoutData(textLData);
						text.setText("Hello \n\t and\n\t\t  thank \n\t\t\t     you\n\t\t\t\t     for\n\t\t\t\t\t    using \n\t\t\t\t\t\t       this \n\t\t\t\t\t\t             system!");
						text.setFont(SWTResourceManager.getFont("Tahoma", 16, 2, false, false));
						text.setBackground(SWTResourceManager.getColor(0, 0, 64));
						text.setForeground(SWTResourceManager.getColor(255, 255, 198));
						text.setOrientation(SWT.HORIZONTAL);
						{
							cancel = new Button(text, SWT.PUSH | SWT.CENTER);
							cancel.setText("cancel");
							cancel.setBounds(372, 214, 53, 23);
							cancel.addSelectionListener(new SelectionAdapter() {
								public void widgetSelected(SelectionEvent evt) {
									mainCommand.closMainCommand();
									shell.dispose();
								}
							});
						}
					}
				}
			}
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	


}
